<?php
/**
 * @title:
 * @desc:
 * @since：2019/7/2 0002  上午 9:35
 */
namespace app\http\middleware;

use think\facade\Config;
use app\common\tools\ReturnCode;
use app\common\logic\Base;

class AdminAuth extends Base
{
    public function handle($request, \Closure $next) {

        $module     = $request->module();
        $controller = $request->controller();
        $action     = $request->action();
        $rule       = $module ."/". $controller  ."/". $action;

        $notRequire = Config::get('apiadmin.NOT_REQUIRE');
        $header = Config::get('apiadmin.CROSS_DOMAIN');

        if (in_array($rule, $notRequire)) {
            return $next($request);
        }

        $MtToken = $request->header('MtToken', '');

        if ($MtToken) {
            $userInfo = cache('Login:' . $MtToken);
            $userInfo = json_decode($userInfo, true);
            if (!$userInfo || !isset($userInfo['id'])) {

                return $this->buildFailed(ReturnCode::TOKEN_INVALID[0], ReturnCode::TOKEN_INVALID[1]);//->header($header);
            } else {
                $request->API_ADMIN_USER_INFO = $userInfo;
            }

            return $next($request);
        } else {
            return $this->buildFailed(ReturnCode::TOKEN_NULL[0], ReturnCode::TOKEN_NULL[1]);//->header($header);
        }

    }
}